#include "searcher.hpp"
#include "cc_httplib.h"//cpp-httplib-v0.7.15/httplib.h

const std::string input = "date/raw_html/raw.txt";
const std::string root_path = "./wwwroot";


int main()
{
    ns_searcher::Searcher search;
    search.InitSearch(input);

    httplib::Server svr; 
    //设置wwwroot（网页）根目录
    svr.set_base_dir(root_path.c_str());
    //获取网页资源
    svr.Get("/s",[&search](const httplib::Request &req, httplib::Response &rsp){//Get
        if(!req.has_param("word"/*参数名字*/)/*表面用户是否有参数*/){
            rsp.set_content("必须要有搜索关键字！","text/plain; charset=utf-8"
            /*Content-Type对照表，就是返回的不同内容，你想要他是什么类型，plain就是普通文本*/);
            return;
        }
        std::string word = req.get_param_value("word");/*这两word不是一个东西哈*/
        //std::cout<<"用户当前在搜索："<<word<<std::endl;
        LOG(NORMAL,"用户搜索的： " + word);
        std::string json_string;
        search.Search(word,&json_string);
        rsp.set_content(json_string,"application/json");
        //rsp.set_content("你好，世界！","texy/plain; charset=utf-8");
    });//Get

    LOG(NORMAL,"服务器启动成功...");
    svr.listen("0.0.0.0",8081);
    return 0;
}